package ustc.EA.web.action;

import java.io.IOException;
 
import java.util.ArrayList;
 
import java.util.HashMap;
 
import java.util.List;
import java.util.Map;

import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import ustc.EA.excel.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ExcelAction extends ActionSupport {
	private static JXLOperator operator=new JXLOperator();
	public final static String USERNAME="username";
	private String username;
	private String password;
	private String email;
	
	private String[] projects;
	private Map<String, String[]> tables;
	private Map<String, String> tableDetails;
	
	private String projectName;
	private String sheetName;
	private String headerline;
 
	private String colName;
	private String defaultValue;
	private String lineNum;
	
	private List<RowData> lists;
	
	public String[] getProjects() {
		return projects;
	}

	public void setProjects(String[] projects) {
		this.projects = projects;
	}

	public Map<String, String[]> getTables() {
		return tables;
	}

	public void setTables(Map<String, String[]> tables) {
		this.tables = tables;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public String getHeaderline() {
		return headerline;
	} 

	public void setHeaderline(String headerline) {
		this.headerline = headerline;
	}
	private String[] header;
	private String[] datas;
	private String[] field;
	private String rowNo;
	 

	public String getRowNo() {
		return rowNo;
	}

	public void setRowNo(String rowNo) {
		this.rowNo = rowNo;
	}

	public String[] getDatas() {
		return datas;
	}

	public void setDatas(String[] datas) {
		this.datas = datas;
	}

	public String[] getHeader() {
		return header;
	}

	public void setHeader(String[] header) {
		this.header = header;
	}

	public List<RowData> getLists() {
		return lists;
	}

	public void setLists(List<RowData> lists) {
		this.lists = lists;
	}

 
	
	public String login(){
 
		if(operator.login(username, password)){
			
			setProjects(operator.listProject(username));
			tables = new HashMap<String, String[]>();
			for(String project : projects){
				
				try {
					tables.put(project, operator.listSheets(username, project));
				} catch (BiffException e) {
					
					e.printStackTrace();
				} catch (IOException e) {
					
					e.printStackTrace();
				} catch (RowsExceededException e) {
					
					e.printStackTrace();
				} catch (WriteException e) {
					
					e.printStackTrace();
				}
			}
			
			getSession().put(USERNAME, username);
			return SUCCESS;
		}
		return INPUT;
		
	}
	
	public String register(){
		
		if(operator.register(username, password, email)){
			return SUCCESS;
		}else{
			return INPUT;
		}
		
	}
	
	public String createProject() throws RowsExceededException, WriteException, BiffException{
		if(isLogin()){
			try {
				operator.createProject((String) getSession().get(USERNAME), projectName);
				return SUCCESS;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return INPUT;
	}
	
	public String createSheet(){
		if(isLogin()){
			try {
			//	String[] headers=headerline.split(",");
				operator.createSheet((String) getSession().get(USERNAME), projectName, sheetName, field);
				
				return listdata();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (RowsExceededException e) {
				e.printStackTrace();
			} catch (WriteException e) {
				e.printStackTrace();
			} catch (BiffException e) {
				e.printStackTrace();
			}
		}
		return INPUT;
	}
	
	public String deleteSheet(){
		
		try {
			operator.delSheet((String) getSession().get(USERNAME), projectName, sheetName);
		} catch (BiffException e) {
			
			e.printStackTrace();
		} catch (WriteException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String listdata(){
		try {
			header= operator.listSheetHeader((String) getSession().get(USERNAME), projectName, sheetName);
			lists=operator.readData((String) getSession().get(USERNAME), projectName, sheetName, 1, 5);
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String foreAddData(){
		try {
			header= operator.listSheetHeader((String) getSession().get(USERNAME), projectName, sheetName);
		} catch (BiffException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String insertData(){
		try {
			operator.insertData((String) getSession().get(USERNAME), projectName, sheetName, datas);
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listdata();
	}
	public String listTables(){
		
		try {
			String[] tables = operator.listSheets((String)getSession().get(USERNAME), projectName);
			tableDetails = new HashMap<String, String>();
			for(String table : tables){
				
				String[] details = operator.listSheetHeader((String)getSession().get(USERNAME) , projectName, table);
				StringBuffer sb = new StringBuffer();
				for(String detail : details){
					sb.append(detail);
					sb.append(",");
				}
				
				tableDetails.put(table, sb.toString());
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			
			e.printStackTrace();
		} catch (WriteException e) {
			
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	public String deleteData(){
		try {
			if(operator.delRow((String)getSession().get(USERNAME), projectName, sheetName, rowNo)){
				return SUCCESS;
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		 return INPUT;
	}
	
	public String delColumn(){
		try {
			operator.delColumn((String)getSession().get(USERNAME), projectName, sheetName, colName);
		} catch (BiffException e) {
			
			e.printStackTrace();
		} catch (WriteException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String addColumn(){
		
		try {
			operator.addColumn((String)getSession().get(USERNAME), projectName, sheetName, colName, defaultValue);
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String editRow(){
		
		try {
			operator.updateSheet((String)getSession().get(USERNAME), projectName, sheetName, lineNum, field);
		} catch (BiffException e) {
			
			e.printStackTrace();
		} catch (WriteException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String delProject(){
		operator.delProject((String)getSession().get(USERNAME), projectName);
		return SUCCESS;
	}
	
	protected boolean isLogin(){
		return getSession().containsKey(USERNAME);
	}
	
	protected Map getSession(){
		 ActionContext actionContext = ActionContext.getContext();
	       Map session = actionContext.getSession();
	       return session;
	}

	public Map<String, String> getTableDetails() {
		return tableDetails;
	}

	public void setTableDetails(Map<String, String> tableDetails) {
		this.tableDetails = tableDetails;
	}

	public String[] getField() {
		return field;
	}

	public void setField(String[] field) {
		this.field = field;
	}

	public String getColName() {
		return colName;
	}

	public void setColName(String colName) {
		this.colName = colName;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getLineNum() {
		return lineNum;
	}

	public void setLineNum(String lineNum) {
		this.lineNum = lineNum;
	}

	
}
